package love.contact;

import java.io.File;

import love.contact.data.Setting;
import love.contact.engine.Engine;
import love.contact.engine.PhoneService;
import love.contact.engine.TheApplication;
import wwk.common.util.DeviceInfo;
import wwk.common.util.FileUtil;
import wwk.common.util.SysUtil;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

	private RadioGroup radioGroup;
	private TabHost tabHost;
	private long lastBackPressedTime = 0; 
	private Toast toast=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		DeviceInfo device=new DeviceInfo(this);
		device.getJsonString();
		
		TheApplication theApp=(TheApplication)getApplication();

		Setting setting=new Setting();
		setting.load(this);
		int packageVCode=SysUtil.getVersionCode(this);
		if(setting.isRequireRquestConfig(packageVCode))
		{
			theApp.updateController.requestConfig(this);
		}

        Intent intent=getIntent();
        Uri uri=(Uri)intent.getData();
        if(uri!=null)
        {
	        	String path=uri.getPath(); 
	        	Log.d("getPath",path);
	        	
	        	//ctt file
			theApp.engine.pendingFilePathName=path;
        }
        else
        {
        		Bundle bundle=intent.getExtras();
        		if(bundle!=null)
        		{
        			String action=bundle.getString("action");
	    	    		if(action!=null && action.equalsIgnoreCase("install"))
	    	    		{
	    	    			theApp.updateController.install(this);
	    	    		}
        		}
        }

		setContentView(R.layout.maintabs);
		
		initData();
		
		findView();
	}
	
	protected void onNewIntent(Intent intent) 
	{
		super.onNewIntent(intent);
        
        TheApplication theApp=(TheApplication)getApplication();
	    if(theApp.engine.pendingFilePathName!=null)
	    {
	    		((RadioButton) findViewById(R.id.imexportRadio)).setChecked(true);
	    }
	    
	    Bundle bundle=intent.getExtras();
		if(bundle!=null)
		{
	    		String action=bundle.getString("action");
	    		if(action!=null && action.equalsIgnoreCase("install"))
	    		{
	    			theApp.updateController.install(this);
	    		}
		}
	}

	@Override
	public void onBackPressed() 
	{
		Log.d("NavigationActivity onBackPressed()","");
		
		long theTime=System.currentTimeMillis();
		if(theTime-lastBackPressedTime<=2000)
		{
			toast.cancel();
			super.onBackPressed();
		}
		else
		{
			lastBackPressedTime=theTime;
			toast=Toast.makeText(this,this.getString(R.string.twiceExit), Toast.LENGTH_SHORT);
			toast.show();
		}
	}
	
	private void initData()
    {
    		FileUtil.setFilesDir(this);

        TheApplication theApp=(TheApplication)getApplication();
        theApp.engine.getPhoneBook().openOrCreateDatabaseWithReferenceCount(this);
        
        Setting setting=new Setting();
        setting.load(this);
        if(setting.isMonitorServiceEnabled())
        {
        		PhoneService.stopService(this);
        		PhoneService.startService(this);
        }
        
        //create path for export
  		String sdCard=FileUtil.getSdCardDir();
  		String path=sdCard+File.separator+SysUtil.getPackageName(this);
  		FileUtil.createPath(path);
    }

	private void findView() 
	{
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
	    radioGroup.setOnCheckedChangeListener(this);
	        
		tabHost = getTabHost();
		
        tabHost.addTab(tabHost.newTabSpec("mygroup").setIndicator("mygroup").setContent(new Intent(this, MyGroupActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("imexport").setIndicator("imexport").setContent(new Intent(this, ImExportActivity.class)));
        //tabHost.addTab(tabHost.newTabSpec("square").setIndicator("Square").setContent(new Intent(this, VideoPlayerActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("more").setIndicator("more").setContent(new Intent(this, MoreActivity.class)));

        TheApplication theApp=(TheApplication)getApplication();
	    if(theApp.engine.pendingFilePathName!=null)
	    {
	    	((RadioButton) findViewById(R.id.imexportRadio)).setChecked(true);
	    }
	    else
	    {
	    	((RadioButton) findViewById(R.id.myGroupRadio)).setChecked(true);
	    }
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) 
	{
		switch (checkedId) 
		{
		case R.id.myGroupRadio:
			tabHost.setCurrentTabByTag("mygroup");
			break;
		case R.id.imexportRadio:
			tabHost.setCurrentTabByTag("imexport");
			break;
		case R.id.festivalRadio:
			tabHost.setCurrentTabByTag("imexport");
			break;
		case R.id.moreRadio:
			tabHost.setCurrentTabByTag("more");
			break;
		}
	}

}
